// Copyright (c) Appeon. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. 
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.

using System;
using System.Collections.Generic;

namespace SnapObjects.Data
{
    internal class MapperContext : IMapperContext
    {
        private readonly SqlModelMapper _modelMapper;
        private readonly IDictionary<Type, ICompositeModelAccessor> _compositeModelAccessors;

        public MapperContext(SqlModelMapper modelMapper)
        {
            _modelMapper = modelMapper;
            _compositeModelAccessors = new Dictionary<Type, ICompositeModelAccessor>();
        }

        public ISqlModelMapper ModelMapper => _modelMapper;

        public IModelEntry GetModelEntry(object model)
        {
            return _modelMapper.GetModelEntry(model);
        }

        public ICompositeModelAccessor GetCompositeModelAccessor(Type modelType)
        {

            if (!_compositeModelAccessors.TryGetValue(modelType, out var accessor))
            {
                accessor = CompositeModelAccessorFactory.Create(modelType);
                _compositeModelAccessors.Add(modelType, accessor);
            }

            return accessor;
        }
    }
}
